package com.baishui.core.type.classreading;

import com.baishui.core.annotation.AnnotationAttributes;
import com.baishui.core.type.AnnotationMetadata;
import org.springframework.asm.AnnotationVisitor;
import org.springframework.asm.Type;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * 针对class存在注解的ReadingVisitor
 */
public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor implements AnnotationMetadata {

	/**
	 * class中存在的注解信息
	 */
	private final Set<String> annotationSet = new LinkedHashSet<String>(4);
	/**
	 * 注解中的属性 <注解名 - <注解key - 注解value>>
	 */
	private final Map<String, AnnotationAttributes> attributeMap = new LinkedHashMap<String, AnnotationAttributes>(4);
	
	public AnnotationMetadataReadingVisitor() {
		
	}

	/**
	 * 重写
	 * @param desc 注解的 全限列名，eg. com.baishui.beans.factory.annotation.Autowired
	 * @param visible
	 * @return
	 */
	@Override
	public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
		String className = Type.getType(desc).getClassName();
		this.annotationSet.add(className);

		/**
		 * 针对注解中的属性的  ReadingVisitor
		 */
		return new AnnotationAttributesReadingVisitor(className, this.attributeMap);
	}
	public Set<String> getAnnotationTypes() {
		return this.annotationSet;
	}

	public boolean hasAnnotation(String annotationType) {
		return this.annotationSet.contains(annotationType);
	}

	public AnnotationAttributes getAnnotationAttributes(String annotationType) {
		return this.attributeMap.get(annotationType);
	}

	
	
}
